package com.xintai.sparepart.spareandpart.service.impl;

import java.util.Date;
import java.util.List;

import com.xintai.common.constant.Constants;
import com.xintai.common.constant.UserConstants;
import com.xintai.common.utils.SecurityUtils;
import com.xintai.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xintai.sparepart.spareandpart.mapper.BeiTypesMapper;
import com.xintai.sparepart.spareandpart.domain.BeiTypes;
import com.xintai.sparepart.spareandpart.service.IBeiTypesService;

/**
 * 备品备件货品分类Service业务层处理
 *
 * @author xintai
 * @date 2020-12-23
 */
@Service
public class BeiTypesServiceImpl implements IBeiTypesService
{
    @Autowired
    private BeiTypesMapper beiTypesMapper;

    /**
     * 查询备品备件货品分类
     *
     * @param typeId 备品备件货品分类ID
     * @return 备品备件货品分类
     */
    @Override
    public BeiTypes selectBeiTypesById(Long typeId)
    {
        return beiTypesMapper.selectBeiTypesById(typeId);
    }

    /**
     * 查询备品备件货品分类列表
     *
     * @param beiTypes 备品备件货品分类
     * @return 备品备件货品分类
     */
    @Override
    public List<BeiTypes> selectBeiTypesList(BeiTypes beiTypes)
    {
        return beiTypesMapper.selectBeiTypesList(beiTypes);
    }

    /**
     * 新增备品备件货品分类
     *
     * @param beiTypes 备品备件货品分类
     * @return 结果
     */
    @Override
    public int insertBeiTypes(BeiTypes beiTypes)
    {
        //通过上级
        BeiTypes Types1 = selectBeiTypesById(beiTypes.getTypeParentid());
        beiTypes.setTypeTime(new Date());
        beiTypes.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        beiTypes.setTypeAllName(Types1.getTypeAllName()+","+beiTypes.getTypeParentid());
        return beiTypesMapper.insertBeiTypes(beiTypes);
    }

    /**
     * 修改备品备件货品分类
     *
     * @param beiTypes 备品备件货品分类
     * @return 结果
     */
    @Override
    public int updateBeiTypes(BeiTypes beiTypes)
    {
        return beiTypesMapper.updateBeiTypes(beiTypes);
    }

    /**
     * 批量删除备品备件货品分类
     *
     * @param typeIds 需要删除的备品备件货品分类ID
     * @return 结果
     */
    @Override
    public int deleteBeiTypesByIds(Long[] typeIds)
    {
        return beiTypesMapper.deleteBeiTypesByIds(typeIds, Constants.FAIL);
    }

    /**
     * 删除备品备件货品分类信息
     *
     * @param typeId 备品备件货品分类ID
     * @return 结果
     */
    @Override
    public int deleteBeiTypesById(Long typeId)
    {
        return beiTypesMapper.deleteBeiTypesById(typeId,Constants.FAIL);
    }

    /**
     * 是否重名
     * @param beiTypes
     * @return
     */
    @Override
    public String checkTypeNameUnique(BeiTypes beiTypes) {
        Long typeId = StringUtils.isNull(beiTypes.getTypeId()) ? -1L : beiTypes.getTypeId();
        BeiTypes info = beiTypesMapper.checkTypeNameUnique(beiTypes.getTypeName(),beiTypes.getTypeParentid());
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询未停用的子分类
     * @param typeId
     * @return
     */
    @Override
    public int selectNormalChildrenTypeById(Long typeId) {
        return beiTypesMapper.selectNormalChildrenTypeById(typeId);
    }

    @Override
    public boolean hasChildByTypeId(Long typeIds) {
        boolean fash = false;
        int i = beiTypesMapper.hasChildByTypeId(typeIds);
        if (i>0){
            return fash = true;
        }
        return fash;
    }

    /**
     * 分类下有没有货品
     * @param typeIds
     * @return
     */
    @Override
    public boolean checkTypeExistProducts(Long typeIds) {
        if (beiTypesMapper.checkTypeExistProducts(typeIds)>0){
            return true;
        }
        return false;
    }


}
